﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Application.ActionResults;
using MethodWorx.CMS.UI.Areas.Dashboard.Models;
using MethodWorx.CMS.TemplateParser.Installer;
using System.Web.UI.WebControls;
using MethodWorx.CMS.Providers;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.UI.Application;

namespace MethodWorx.CMS.UI.Areas.Dashboard.Controllers
{
    public class TemplateGalleryController : ApplicationController
    {
        //
        // GET: /Dashboard/Gallery/
        private ICmsDataProvider cmsDataProvider;
        private ContentService content;

        public TemplateGalleryController(ICmsDataProvider cmsDataProvider, ContentService content)
        {
            this.cmsDataProvider = cmsDataProvider;
            this.content = content;
        }

        public ActionResult Index(bool ? dialogMode)
        {
            var files = Directory.GetFiles(Server.MapPath("~/App_Data/Templates"));
            return View(new TemplateGalleryModel(files, dialogMode ?? false, this.CurrentUser));
        }

        public JsonResult GetTemplates(string category)
        {
            var files = Directory.GetFiles(Server.MapPath("~/App_Data/Templates"));
            var model = new TemplateGalleryModel(files, false, this.CurrentUser);
            List<object> items = new List<object>();
            foreach(InstallerEngine engine in model.GetInstallers())
            {
                var parser = engine.GetParser();
                items.Add(new {Filename = Path.GetFileName(engine.Filename), Name = parser.Name, Author = parser.Author});
            }

            return Json(items.ToArray(), JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        public FileResult Preview(string filename)
        {
            var model = new TemplateGalleryModel(new string[] { Server.MapPath("~/App_Data/Templates/" + filename) }, false, this.CurrentUser);
            return File(model.GetInstallers().First().GetParser().Preview, "image/png");
        }

        public ActionResult Install(string filename, int? step, string submit, FormCollection form, bool?  macro)
        {
            string [] filenames;
            if((macro ?? false))
                filenames = new string[] {Server.MapPath("~/App_Data/Macros/" + filename)};
            else
                filenames = new string[] {Server.MapPath("~/App_Data/Templates/" + filename)};

            var model = new TemplateGalleryModel(filenames, false, this.CurrentUser);

            var installer = model.GetInstallers().First();
            var parser = installer.GetParser();

            //
            //  create the model for this view
            var installModel = new TemplateInstallModel(parser, step ?? 0, this.content, macro ?? false);
            ActionResult result = null;

            switch(submit ?? "")
            {
                
                case "install":
                    installModel.UpdateFromFormCollection(form, this.ModelState);
                    if (this.ModelState.IsValid)
                        installModel.Install(this.cmsDataProvider, this.ModelState);
                    result = View("InstallationComplete", installModel);
                    break;
                case "next":
                    installModel.UpdateFromFormCollection(form, this.ModelState);
                    if (this.ModelState.IsValid)
                        installModel.NextStep();
                    result = View(installModel);
                    break;
                case "cancel":
                    result = new CloseDialogResult();
                    break;
                default:
                    result = View(installModel);
                    break;
            }

            return result;
        }
    }
}
